
#ifndef XDNS_HANDLER_H
#define XDNS_HANDLER_H

#include "xdns.h"
#include "xdns-server-state-listener.h"
#include <map>
#include <string>

class XdnsQuery;
class XdnsServerState;

class XdnsHandlerImpl : public XdnsHandler,
                        public XdnsServerStateListener
{
public:
    /*! XdnsHandlerImpl constructor

        @param serverState State of a DNS server. This function takes the
               ownership of this pointer.
    */
    XdnsHandlerImpl(XdnsServerState* serverState);
    virtual ~XdnsHandlerImpl();

    /* See xdns.h for description. */
    virtual XdnsError GetHostByName(const std::string& name,
                                    XdnsListener* listener,
                                    long* queryId);
    virtual XdnsError GetHostByAddress(const struct in_addr* ipAddress,
                                       XdnsListener* listener,
                                       long* queryId);
    virtual void CancelQuery(long queryId);
    virtual XdnsError GetProcessingFds(int* nfds,
                                       fd_set* readfds,
                                       fd_set* writefds);
    virtual XdnsError Process(fd_set* readfds,
                              fd_set* writefds);
    virtual std::string GetLastErrorDescription() const;

    /* See xdns-server-state-listener.h for description. */
    virtual void ReplyReceived(long queryId,
                               XdnsError error,
                               const XdnsData* data,
                               size_t startOffset,
                               const XdnsHeaderInfo* header);

private:
    // Copy constructor and assignment operator are not supported.
    XdnsHandlerImpl(const XdnsHandlerImpl& anotherHandler);
    XdnsHandlerImpl& operator=(const XdnsHandlerImpl& anotherHandler);

    XdnsError GetValidQueryId(long& queryId,
                              std::map<long, XdnsQuery*>::iterator& iter);
    long NextQueryId();
    std::string LongToString(long value);

    long m_nextQueryId;
    XdnsServerState* m_serverState;
    std::string m_lastError;
    std::map<long, XdnsQuery*> m_queries;
};

#endif  // XDNS_HANDLER_H

